import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  {
    path: '/employee',
    component: Layout,
    name: 'Employee',
    redirect: '/employee/activeTable',
    meta: { title: '员工管理', icon: 'staff' },
    children: [{
      path: 'activeTable',
      name: 'ActiveTable',
      component: () => import('@/views/employee/activeTable/index'),
      meta: { title: '在职员工列表' }
    }, {
      path: 'deletedTable',
      name: 'DeletedTable',
      component: () => import('@/views/employee/deletedTable/index'),
      meta: { title: '离职员工列表' }
    }
    ]
  },

  {
    path: '/user',
    component: Layout,
    name: 'User',
    redirect: '/user/activeTable',
    meta: { title: '用户管理', icon: 'user' },
    children: [{
      path: 'activeTable',
      name: 'ActiveTable',
      component: () => import('@/views/user/activeTable/index'),
      meta: { title: '有效用户列表' }
    }, {
      path: 'deletedTable',
      name: 'DeletedTable',
      component: () => import('@/views/user/deletedTable/index'),
      meta: { title: '注销用户列表' }
    }
    ]
  },
  {
    path: '/fridge',
    component: Layout,
    name: 'Fridge',
    redirect: '/fridge/activeTable',
    meta: { title: '冰箱管理', icon: 'fridge' },
    children: [{
      path: 'activeTable',
      name: 'ActiveTable',
      component: () => import('@/views/fridge/activeTable/index'),
      meta: { title: '冰箱管理' }
    }, {
      path: 'deletedTable',
      name: 'DeletedTable',
      component: () => import('@/views/fridge/deletedTable/index'),
      meta: { title: '黑名单管理' }
    }
    ]
  },
  {
    path: '/food',
    component: Layout,
    name: 'Food',
    redirect: '/food/default',
    meta: { title: '食物管理', icon: 'food' },
    children: [{
      path: 'default',
      name: 'Default',
      component: () => import('@/views/food/default/index'),
      meta: { title: '默认食物管理' }
    }, {
      path: 'custom',
      name: 'Custom',
      component: () => import('@/views/food/custom/index'),
      meta: { title: '自定义食物管理' }
    }
    ]
  },
  {
    path: '/dish',
    component: Layout,
    name: 'Dish',
    redirect: '/dish/manage',
    meta: { title: '菜谱管理', icon: 'dish' },
    children: [{
      path: 'manage',
      name: 'Manage',
      component: () => import('@/views/dish/manage/index'),
      meta: { title: '有效列表' }
    }, {
      path: 'apply',
      name: 'Apply',
      component: () => import('@/views/dish/apply/index'),
      meta: { title: '申请列表' }
    }
    ]
  },
  {
    path: '/foodType',
    component: Layout,
    name: 'FoodType',
    redirect: '/foodType/activeTable',
    meta: { title: '食物种类', icon: 'foodType' },
    children: [{
      path: 'activeTable',
      name: 'ActiveTable',
      component: () => import('@/views/foodType/activeTable/index'),
      meta: { title: '食物种类管理' }
    }
    ]
  },
  {
    path: '/dishType',
    component: Layout,
    name: 'DishType',
    redirect: '/dishType/activeTable',
    meta: { title: '菜谱种类', icon: 'foodType' },
    children: [{
      path: 'activeTable',
      name: 'ActiveTable',
      component: () => import('@/views/dishType/activeTable/index'),
      meta: { title: '菜谱种类管理' }
    }
    ]
  },
  {
    path: '/unit',
    component: Layout,
    name: 'Unit',
    redirect: '/unit/activeTable',
    meta: { title: '计量单位管理', icon: 'unit' },
    children: [{
      path: 'activeTable',
      name: 'ActiveTable',
      component: () => import('@/views/unit/activeTable/index'),
      meta: { title: '计量单位管理' }
    }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
